#! /usr/bin/sdlbrt

finp$="picture.png":clstr=1:md=1:xed=320:yed=200

'if argc>2 then:finp$=argv(2):end if

'if argc>=3 then:finp$=command$(3):end if
'if argc>=4 then:xed=val(command$(4)):end if
'if argc>=5 then:yed=val(command$(5)):end if
'if argc>=6 then:clstr=val(command$(6)):end if
'if argc>=7 then:md=val(command$(7)):end if



shell("rm _t*.png _v*.*")

shell("convert "+finp$+" -posterize 8 _v1.png")
shell("convert _v1.png -colors 32 _v2.png")
shell("convert _v2.png -posterize 8 _v5.png")
shell("convert _v5.png -posterize 16 _v6.png")
shell("convert _v6.png -unique-colors -scale 400% _v3.png")

wdwd=1
setdisplay(xed*4,yed*4,32,wdwd):paper(8^8-1):ink(0):pen(0):cls

loadimage("_v3.png",1):pasteicon(0,0,1)

for i=0 to 31
  u=point(i*4,0)
  b=bitwiseand(u,0xEE)/2:u=int(u/256)
  g=bitwiseand(u,0xEE)/2:u=int(u/256)
  r=bitwiseand(u,0xEE)/2
  ink(rgb(r,g,b))
  bar(128+(i*4),0,3+128+(i*4),3)
  next

grab(1,0,0,256,4):saveimage("_v4.bmp",1):shell("convert _v4.bmp _v4.gif && rm _v4.bmp")

shell("convert "+finp$+" -scale "+str$(xed*4)+"x"+str$(yed*4)+"! _t2.png")
'shell("convert "+finp$+" +dither -colors 32 _t4.png")
'shell("convert _t4.png +dither -posterize 8 _t5.png")
'shell("convert _t5.png +dither -colors 32 _t6.png")
'shell("convert _t2.png -quantize CMY -remap _t6.png -dither Riemersma _t3.png")
shell("convert _t2.png -quantize CMY -remap _v4.gif -dither Riemersma _t3.png")

loadimage("_t3.png",1):pasteicon(0,0,1)
shell("rm _t*.png")

dim p1[16],k1[16]

function grayscale(ct):
  cq=ct
  bq=bitwiseand(cq,255):cq=int(cq/256)
  gq=bitwiseand(cq,255):cq=int(cq/256)
  rq=bitwiseand(cq,255):cq=int(cq/256)
  return int(((rq*30)+(gq*59)+(bq*11))/100)
  end function

for y2=0 to yed-1
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    for y1=0 to 3:for x1=0 to 3
      xq=y1*4+x1
      p1[xq]=point(x2*4+x1,y2*4+y1)
      next:next
    for i=0 to 15
      k1[i]=grayscale(p1[i])
      next
    for j=0 to 14:for i=0 to 14
      if k1[i]>k1[i+1] then:
        tm=k1[i]:k1[i]=k1[i+1]:k1[i+1]=tm
        tm=p1[i]:p1[i]=p1[i+1]:p1[i+1]=tm
        end if
      next:next

    if clstr=0 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 6]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 9]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[10]):dot(x2*4+1,y2*4+1)
    ink(p1[ 3]):dot(x2*4+2,y2*4+1)
    ink(p1[13]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[ 7]):dot(x2*4+3,y2*4+2)
    ink(p1[ 2]):dot(x2*4+0,y2*4+3)
    ink(p1[12]):dot(x2*4+1,y2*4+3)
    ink(p1[ 5]):dot(x2*4+2,y2*4+3)
    ink(p1[11]):dot(x2*4+3,y2*4+3)
    end if
    
    if clstr=1 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 4]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 8]):dot(x2*4+2,y2*4+0)
    ink(p1[12]):dot(x2*4+3,y2*4+0)
    ink(p1[10]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 2]):dot(x2*4+2,y2*4+1)
    ink(p1[ 6]):dot(x2*4+3,y2*4+1)
    ink(p1[ 1]):dot(x2*4+0,y2*4+2)
    ink(p1[ 5]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[11]):dot(x2*4+0,y2*4+3)
    ink(p1[15]):dot(x2*4+1,y2*4+3)
    ink(p1[ 3]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=1 and md=1 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 4]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 8]):dot(x2*4+2,y2*4+0)
    ink(p1[12]):dot(x2*4+3,y2*4+0)
    ink(p1[10]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 2]):dot(x2*4+2,y2*4+1)
    ink(p1[ 6]):dot(x2*4+3,y2*4+1)
    ink(p1[ 0]):dot(x2*4+0,y2*4+2)
    ink(p1[ 4]):dot(x2*4+1,y2*4+2)
    ink(p1[ 8]):dot(x2*4+2,y2*4+2)
    ink(p1[12]):dot(x2*4+3,y2*4+2)
    ink(p1[10]):dot(x2*4+0,y2*4+3)
    ink(p1[14]):dot(x2*4+1,y2*4+3)
    ink(p1[ 2]):dot(x2*4+2,y2*4+3)
    ink(p1[ 6]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=2 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[10]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 1]):dot(x2*4+2,y2*4+0)
    ink(p1[11]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 5]):dot(x2*4+2,y2*4+1)
    ink(p1[15]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[ 2]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[ 3]):dot(x2*4+3,y2*4+2)
    ink(p1[12]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[13]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=3 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[12]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 3]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 8]):dot(x2*4+0,y2*4+1)
    ink(p1[ 4]):dot(x2*4+1,y2*4+1)
    ink(p1[11]):dot(x2*4+2,y2*4+1)
    ink(p1[ 7]):dot(x2*4+3,y2*4+1)
    ink(p1[ 2]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[10]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[ 9]):dot(x2*4+2,y2*4+3)
    ink(p1[ 5]):dot(x2*4+3,y2*4+3)
    end if

    x1=x2 mod 4:y1=y2 mod 4
    u=point(x2*4+x1,y2*4+y1)
    ink(u):dot(x2,y2)
    next:next



loadimage("_v4.gif",1):pasteicon(xed,0,1)


for y=0 to yed-1
  for x=0 to xed-1
    cf=0
    for c=0 to 63
      if point (x,y)=point (xed+(c*4),0) then
        cf=c
        end if
      next
    ink (point ( xed+((cf mod 32)*4),0 ))
    dot(x,yed+y)
    ink (0x000000):if cf>31 then:ink (0xFFFFFF):end if
    dot(xed+x,yed+y)
    next:next

'waitkey

grab(1,0,0,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp" )

grab(1,0,yed,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+"_a.ilbm && rm _.bmp" )
grab(1,xed,yed,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+"_b.ilbm && rm _.bmp" )

grab(1,0,yed,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+"_a.png && rm _.bmp" )
grab(1,xed,yed,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+"_b.png && rm _.bmp" )


shell("rm _t*.png _v*.*")

'shell("convert "+finp$+".png _.ppm && ppmtoilbm _.ppm > "+finp$+".pi1 && rm _.ppm")
'waitkey

